package com.zwj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.zwj.bo.CourseInfoBo;
import com.zwj.dto.PaginationDate;
import com.zwj.dto.Result;
import com.zwj.dto.ReturnDate;
import com.zwj.entity.CourseInfoEntity;
import com.zwj.entity.CourseNodeEntity;
import com.zwj.entity.SysRoleEntity;
import com.zwj.entity.SysUserEntity;
import com.zwj.enums.IsDeletedEnum;
import com.zwj.enums.SexEnum;
import com.zwj.enums.StatusEnum;
import com.zwj.mapper.CourseInfoMapper;
import com.zwj.mapper.CourseNodeMapper;
import com.zwj.mapper.SysUserMapper;
import com.zwj.mapper.UserRoleMapper;
import com.zwj.service.CourseInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.DataCheckUtils;
import com.zwj.utils.DateUtils;
import com.zwj.utils.PaginationUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwj
 * @since 2021-11-02
 */
@Service
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfoEntity> implements CourseInfoService {
    private final CourseInfoMapper courseInfoMapper;
    private final CourseNodeMapper courseNodeMapper;
    public CourseInfoServiceImpl(CourseInfoMapper courseInfoMapper,CourseNodeMapper courseNodeMapper) {
        this.courseInfoMapper = courseInfoMapper;
        this.courseNodeMapper = courseNodeMapper;
    }
    @Override
    public Result selectCourseInfo(CourseInfoBo courseInfoBo) {
        //数据校验
        DataCheckUtils.isValid(PaginationUtils.isPagingParameters(courseInfoBo.getNumber(), courseInfoBo.getPage()), "分页参数错误!");
        int page = courseInfoBo.getPage();
        courseInfoBo.setPage(PaginationUtils.getPageIndex(courseInfoBo.getPage(), courseInfoBo.getNumber()));
        //查询项目组评分 mybatis
        List<Object> result = courseInfoMapper.selectCourseInfo(courseInfoBo);
        int sum = ((List<Integer>) result.get(1)).get(0);
        //记录总数为0
        if (sum == 0) return Result.error().message("查询结果为空");
        PaginationDate paginationDate = PaginationUtils.getPaginationDate(courseInfoBo.getNumber(), page, sum);
        //当前页大于总页数
        if (PaginationUtils.isValid(paginationDate))
            return Result.error().message("分页参数错误!");
        List<CourseInfoEntity> courseInfoEntities =  (List<CourseInfoEntity>) result.get(0);
        return Result.ok().data(new ReturnDate(paginationDate,courseInfoEntities ));
    }

    @Override
    public Result updateCourseInfoStatus(CourseInfoBo courseInfoBo) {

        CourseInfoEntity courseInfoEntity = new CourseInfoEntity();
        courseInfoEntity.setStatus(courseInfoBo.getStatus());
        UpdateWrapper<CourseInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseInfoEntity::getId, courseInfoBo.getId());
        int row = courseInfoMapper.update(courseInfoEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑课程状态失败");
        }
        return Result.ok();
    }

    @Override
    public Result deleteCourseInfo(CourseInfoBo courseInfoBo) {
        DataCheckUtils.isNotBlank(courseInfoBo.getId(), "课程id不能为空!");
        CourseInfoEntity courseInfoEntity = new CourseInfoEntity();
        courseInfoEntity.setIsDeleted(IsDeletedEnum.YES.getKey());
        UpdateWrapper<CourseInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseInfoEntity::getId, courseInfoBo.getId()).eq(CourseInfoEntity::getIsDeleted,IsDeletedEnum.NO.getKey());
        int row = courseInfoMapper.update(courseInfoEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("删除课程失败");
        }
        return Result.ok();
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public Result addCourseInfo(CourseInfoEntity courseInfoEntity,String userId) {
        courseInfoEntity.setIsDeleted(IsDeletedEnum.NO.getKey());
        courseInfoEntity.setStatus(StatusEnum.INVALID.getKey());
        courseInfoEntity.setCreateBy(userId);
        courseInfoEntity.setCreateTime(DateUtils.getCurrentTime());
        int row = courseInfoMapper.insert(courseInfoEntity);
        //判断插入结果
        if (row == 0) {
            return Result.error().message("添加课程失败");
        }
        return Result.ok();
    }

    @Override
    public Result updateCourseInfo(CourseInfoEntity courseInfoEntity,String userId) {
        courseInfoEntity.setUpdateBy(userId);
        courseInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        UpdateWrapper<CourseInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(CourseInfoEntity::getId, courseInfoEntity.getId());
        int row = courseInfoMapper.update(courseInfoEntity, updateWrapper);
        //判断修改结果
        if (row == 0) {
            return Result.error().message("编辑课程信息失败");
        }
        return Result.ok();
    }
}
